package com.frame.base;


import com.frame.common.*;
import com.frame.common.exception.AuthException;
import com.frame.common.exception.ServerException;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author: Flying
 * @date: 2018-10-12 14:50
 * @params: 
 * @return 
 */
@ControllerAdvice
@Slf4j
public class MyExceptionHandler {


	/**
	 *
	 * @author Flying
	 * 2018年1月18日 上午10:32:27
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public BaseWrapper<String> serverException(Exception e) {
		if(log.isErrorEnabled()) {
			log.error(RespEnum.SERVER_ERROR.getDesc(), e);
		}
		return new BaseWrapper<>(RespEnum.SERVER_ERROR.getCode(),RespEnum.SERVER_ERROR.getDesc());
	}

	/**
	 *
	 * @author Flying
	 * 2018年1月18日 上午10:32:27
	 * @param e
	 * @return
	 */
	@ExceptionHandler(AuthException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public BaseWrapper<String> authExceptionHandler(AuthException e) {
		return new BaseWrapper<>(e.getCode(),e.getMessage());
	}



	/**
	 * 监听ServerException异常 统一处理
	 * @author Flying
	 * 2018年1月18日 上午10:32:27
	 * @param e
	 * @return
	 */
	@ExceptionHandler(ServerException.class)
	@ResponseBody
	public BaseWrapper<String> serverExceptionHandler(ServerException e) {
		return new BaseWrapper<>(e.getCode(),e.getMessage());
	}

	/**
	 * 监听参数绑定异常 并处理
	 * @param e
	 * @return
	 */
	@ExceptionHandler(BindException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.FORBIDDEN)
	public BaseWrapper<String> bindExceptionHandler(BindException e) {

		if(log.isInfoEnabled()){
			log.info("错误信息：{}", e.getBindingResult().getFieldError().getDefaultMessage());
		}
		return new BaseWrapper<>(RespEnum.REQUEST_PARAM_ERROR.getCode(),e.getBindingResult().getFieldError().getDefaultMessage());
	}



	/**
	 * Spring 参数绑定异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(MissingServletRequestParameterException.class)
	@ResponseBody
	public BaseWrapper<String> missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
		if(log.isInfoEnabled()){
			log.info(e.getParameterName()+"不能为空");
		}
		return new BaseWrapper<>(RespEnum.REQUEST_PARAM_ERROR.getCode(),e.getParameterName()+"不能为空");
	}



	@ExceptionHandler(AuthorizationException.class)
	@ResponseBody
	public BaseWrapper<String> handleAuthorizationException(AuthorizationException e){
		if(log.isInfoEnabled()){
			log.info(e.getMessage(), e);
		}
		return new BaseWrapper<String>(RespEnum.NO_AUTHORITY_PLEASE_CONTACT_THE_ADMINISTRATOR_TO_AUTHORIZE);
	}


	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	public BaseWrapper<String> methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException e) {
		if(log.isInfoEnabled()){
			log.info(e.getBindingResult().getFieldError().getDefaultMessage()+"不能为空");
		}
		return new BaseWrapper<>(RespEnum.REQUEST_PARAM_ERROR);
	}


	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
	public BaseWrapper<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
		if(log.isErrorEnabled()) {
			log.error(RespEnum.SERVER_ERROR.getDesc(), e);
		}
		return new BaseWrapper<>(RespEnum.SYSTEM_AUTHENTICATION);
	}





}
